import { onMounted, reactive, ref, nextTick } from 'vue'
import { taskDetailApi } from '@/api/ReviewBusiness/01Project/project'
import { TaskPageParams, TaskAddType, taskDetailType } from '@/api/ReviewBusiness/01Project/projectType'
import { useRoute } from 'vue-router'
import { getBaselineListApi } from '@/api/ReviewBusiness/01Project/project'
import useInstance from '@/hooks/useInstance';
import { EditType } from '@/type/BaseEnum'
export default function AuditHomePage() {
  const route = useRoute()
  // 1 table 数据 
  const AuditPageList = reactive({
    oldList: [] as any,
    list: [] as any,
    Loading: false,
    currentbase: '',
    BaselineList: [] as any,
    taskBaselineId: '',
    total: 0,
    height: 0,
    taskId: ''
  })
  let searchParams = reactive<taskDetailType>({
    id: '',
    taskBaselineId: ''
  })
  onMounted(async () => {
    searchParams.id = route.query.id as string
    AuditPageList.taskId = route.query.id as string
    await getAuditList(searchParams.id, searchParams.taskBaselineId)

    nextTick(() => {
      getBaseline(route.query.id)
      AuditPageList.height = window.innerHeight - 135
    })
  })
  const tableRowClassName = ({ row, rowIndex }) => {
    if (rowIndex % 2 === 1) {
      return "warning-row";
    } else {
      return "success-row";
    }
  }
  const baselineChange = (item) => {
    if (item) {
      console.log(item);
      console.log(AuditPageList.BaselineList);

      AuditPageList.taskBaselineId = item
      AuditPageList.currentbase = item
      getAuditList(searchParams.id, item)
      let obj = AuditPageList.BaselineList.find((ex) => {
        return ex.id === item
      })
      AuditPageList.currentbase = obj.taskBaseline
    }
  }
  const getBaseline = async (id: any) => {
    const res = await getBaselineListApi({ taskId: id })
    if (res.code === 200) {
      AuditPageList.BaselineList = res.data
      const arr = res.data.find(item => {
        return item.id === AuditPageList.taskBaselineId
      })
      console.log(arr, res.data, '===', id);

      AuditPageList.currentbase = arr.taskBaseline
    }
  }

  const getAuditList = async (id: any, taskBaselineId: any) => {
    AuditPageList.list = []
    const res: any = await taskDetailApi({ id: id, taskBaselineId: taskBaselineId })
    console.log(res.data, '=====');

    if (res.code === 200 && res.data.taskStageVOS) {
      nextTick(() => {
        AuditPageList.taskBaselineId = res.data.taskBaselineId
      })
      AuditPageList.oldList = res.data.taskStageVOS
      res.data.taskStageVOS.map((item, index) => {
        if (item.files && item.files.length > 0) {
          item.files.map(it => {
            AuditPageList.list.push({ ...item, ...it })
          })
        }
      })
    }

  }
  const searchBtn = (row) => {
    AuditPageList.list = []
    if (row.files && row.files.length > 0) {
      row.files.map(it => {
        AuditPageList.list.push({ ...row, ...it })
      })
    }
  }


  const resetBtn = (row) => {
    AuditPageList.list = []
    console.log(row);
    if (row && row.length > 0) {
      row.map((item, index) => {
        if (item.files && item.files.length > 0) {
          item.files.map(it => {
            AuditPageList.list.push({ ...item, ...it })
          })
        }
      })
    }

  }
  const gobackBtn = () => {
    history.go(-1);
  }
  return {
    AuditPageList, getAuditList, searchParams, resetBtn, searchBtn, baselineChange, getBaseline, gobackBtn, tableRowClassName
  }
}